var rzbtn, wjbtn, pagelb, startbtn, taskroot, seltask, seldiv;
var cardheight = 100, cardwidth = 260;//卡片尺寸
var mininterval = 666; //最小间距
var hinterval = mininterval, vinterval = hinterval * 1.5;//卡片间间距，缺省使用最小间距

const ipc = require('electron').ipcRenderer;
let shareobj = require('electron').remote.getGlobal('shareobj');

function init() {
    rzbtn = document.getElementById('rz');
    wjbtn = document.getElementById('wj');
    pagelb = document.getElementById('pageinfo');
    startbtn = document.getElementById('start');
    taskroot = document.getElementById('tasklist');
    calclayout();
    setWaitObj();

    //选中效果div
    seldiv = document.createElement('div');
    seldiv.innerHTML = "√";
    seldiv.setAttribute('class', 'selector');
    if (shareobj['homepageRZ']) {
        rzclick();
    } else {
        wjclick();
    }
}

//计算布局
function calclayout() {
    //控件最小高度
    var minheight = document.getElementById('content').clientHeight + 10 +//控件绝对高度，开始按钮距离底部10
        cardheight * 3 + 10 + mininterval * 2//3行卡片高度，首行卡片距离顶部高度，两个卡片间距高度
    console.log(document.getElementById('content').clientHeight, minheight, window.innerHeight);
    //可用高度
    var availableheight = window.innerHeight - document.getElementById('headline').clientHeight//减去标题高度
    if (availableheight > minheight) {//高度足够
        var percent = 0.3;//使用间距高度所占百分比
        var idle = availableheight - minheight;
        vinterval += idle * percent / 2;
        document.getElementById('maintitle').style.marginTop = idle * (1 - percent) / 2 + 'px';
        startbtn.style.marginTop = idle * (1 - percent) / 4 + 'px';
    }
    document.getElementById('page').style.marginTop = cardheight * 3 + vinterval * 2 + 20 + 10 + 'px'

    //计算水平宽度
    hinterval = vinterval * 1.5;
    var absolutewidth = cardwidth * 3 + hinterval * 2;
    var widthpercent = absolutewidth / document.body.clientWidth * 100;
    document.getElementById('testhr').style.width = widthpercent + '%';
    document.getElementById('subtile').style.left = (100 - widthpercent) / 2 + '%';
    taskroot.style.left = (100 - widthpercent) / 2 + '%';
}

function rzclick() {//认知任务切换处理
    rzbtn.style.backgroundColor = '#8380B3';
    wjbtn.style.backgroundColor = 'rgba(0,0,0,0)';
    //请求认知任务消息
    ipc.send('rz-list');
}

ipc.on('rz-info', function (event, dtlst, pagenum, totalnum) {//认知任务列表信息
    pagelb.innerHTML = pagenum + '/' + totalnum;
    loadtasklst(dtlst);
});

function wjclick() {//问卷任务切换处理
    rzbtn.style.backgroundColor = 'rgba(0,0,0,0)';
    wjbtn.style.backgroundColor = '#8380B3';
    //请求问卷任务消息
    ipc.send('wj-list');
}

ipc.on('wj-info', function (event, dtlst, pagenum, totalnum) {//问卷任务列表信息
    pagelb.innerHTML = pagenum + '/' + totalnum;
    loadtasklst(dtlst);
});

//加载任务列表
function loadtasklst(dtlst) {
    //重新加载列表时，清空当前所选任务
    seltask = null;

    var task, h1, hr, h2, desc;
    taskroot.innerHTML = "";
    for (var idx in dtlst) {
        task = document.createElement('div');
        task.setAttribute('class', 'item');
        task.style.marginLeft = idx % 3 * (hinterval + cardwidth) + 'px'; //每行3个卡片，每个卡片宽度260px
        task.style.marginTop = Math.floor(idx / 3) * (cardheight + vinterval) + 'px';//每个卡片高度100px
        task.id = dtlst[idx].idx;
        task.setAttribute('isfinish', dtlst[idx].isfinish);
        task.setAttribute('onclick', 'taskclick(this)');
        h1 = document.createElement('div');
        h1.setAttribute('class', 'tasknum');
        h1.innerHTML = dtlst[idx].idx;
        hr = document.createElement('hr');
        hr.setAttribute('class', 'taskhr');
        h2 = document.createElement('div');
        h2.setAttribute('class', 'tasktype');
        h2.innerHTML = dtlst[idx].title;
        desc = document.createElement('div');
        desc.setAttribute('class', 'taskdesc');
        desc.innerHTML = dtlst[idx].desc;

        task.appendChild(h1);
        task.appendChild(hr);
        task.appendChild(h2);
        if (dtlst[idx].isfinish) {
            var img = document.createElement('img');
            img.src = 'image/finish.png';
            task.appendChild(img);
        }
        task.appendChild(desc);
        taskroot.appendChild(task);
    }
}

function taskclick(task) {
    if (task.getAttribute('isfinish') == 'false') {//未执行
        if (seltask != null) {//去掉已选中任务
            seltask.removeChild(seldiv);
        }
        task.insertBefore(seldiv, task.lastChild);
        seltask = task;
    }
}

function pageclick(isnext) {//翻页
    ipc.send('next-page', isnext);
}

function starttask() {
    if (seltask != null) {
        startwait();
        ipc.send('load-item', seltask.id);
    }
}

function closeclick() {//退出
    window.close();
}

ipc.on('wait-over', function (event) {
    endwait();
});